{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 关于元组的函数\n",
    "- 以下看代码\n",
    "- 以下函数，对list基本适用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "# len:获取元组的长度\n",
    "t = (1,2,3,4,5)\n",
    "print(len(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# max,min:最大最小值\n",
    "# 如果，列表或元组中有多个最大最小值，则实际打印出哪个\n",
    "print(max(t))\n",
    "print(min(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4, 5)\n",
      "()\n"
     ]
    }
   ],
   "source": [
    "# tuple:转化或创建元组\n",
    "l = [1,2,3,4,5]\n",
    "t = tuple(l)\n",
    "print(t)\n",
    "\n",
    "t = tuple()\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 元组的函数\n",
    "- 基本跟list通用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "4\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# count:计算指定数据出现的次数\n",
    "t = (2,1,2,3,45,1,1,2,)\n",
    "\n",
    "print(t.count(2))\n",
    "\n",
    "# index:求指定元素在元组中的索引位置\n",
    "\n",
    "print(t.index(45))\n",
    "\n",
    "#如果需要查找的数字是多个，则返回第一个\n",
    "print(t.index(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 元组变量交换法\n",
    "- 两个变量交换值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "********************\n",
      "3\n",
      "1\n",
      "********************\n",
      "1\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# 两个变量交换值\n",
    "a = 1\n",
    "b = 3\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "print(\"*\" * 20)\n",
    "\n",
    "# java程序员会这么写：\n",
    "c = a\n",
    "a = b\n",
    "b = c\n",
    "print(a)\n",
    "print(b)\n",
    "print(\"*\" * 20)\n",
    "\n",
    "# python\n",
    "a,b = b,a\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 集合-set\n",
    "- 集合是高中数学中的一个概念\n",
    "- 一堆确定的无序的唯一的数据，集合中每一个数据成为一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'set'>\n",
      "set()\n",
      "{1, 2, 3, 4, 5, 6, 7}\n"
     ]
    }
   ],
   "source": [
    "# 集合的定义\n",
    "s = set()\n",
    "print(type(s))\n",
    "print(s)\n",
    "\n",
    "# 此时，大括号内一定要有值，否则定义出的是一个dict\n",
    "s = {1,2,3,4,5,6,7}\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "# 如果只是用大括号定义，则定义的是一个dict类型\n",
    "d = {}\n",
    "print(type(d))\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 集合的特征\n",
    "- 集合内数据无序，即无法使用索引和分片\n",
    "- 集合内部数据元素具有唯一性，可以用来排除重复数据\n",
    "- 集合内的数据，str，int，float，tuple，冰冻集合等，即内部只能放置可哈希数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 集合序列操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'love', 4, 5, 'i', 'zhangwenhan'}\n",
      "哎呀\n",
      "爱过\n"
     ]
    }
   ],
   "source": [
    "# 成员检测\n",
    "# in,not in\n",
    "s = {4,5,\"i\",\"love\",\"zhangwenhan\"}\n",
    "print(s)\n",
    "\n",
    "if \"love\" in s:\n",
    "    print(\"哎呀\")\n",
    "if \"haha\" not in s:\n",
    "    print(\"爱过\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 集合遍历操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "love 4 5 i zhangwenhan "
     ]
    }
   ],
   "source": [
    "# for循环\n",
    "s = {4,5,\"i\",\"love\",\"zhangwenhan\"}\n",
    "\n",
    "for i in s:\n",
    "    print(i,end = \" \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i -- love -- zhangwenhan\n",
      "4 -- 5 -- 6\n",
      "1 -- 2 -- 3\n",
      "('i', 'love', 'zhangwenhan')\n",
      "(4, 5, 6)\n",
      "(1, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "# 带有元组的集合遍历\n",
    "s = {(1,2,3),(\"i\",\"love\",\"zhangwenhan\"),(4,5,6)}\n",
    "\n",
    "for k,m,n in s:\n",
    "    print(k,\"--\",m,\"--\",n)\n",
    "    \n",
    "for k in s:\n",
    "    print(k)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 集合的内涵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{545, 1, 3, 2, 4, 23, 223}\n",
      "{545, 2, 3, 4, 1, 23, 223}\n"
     ]
    }
   ],
   "source": [
    "# 普通集合内涵\n",
    "# 以下集合在初始化后自动过滤掉重复元素\n",
    "s = {23,223,545,3,1,2,3,4,3,2,3,1,2,4,3}\n",
    "print(s)\n",
    "\n",
    "# 普通集合内涵\n",
    "ss = {i for i in s}\n",
    "print(ss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2, 4}\n"
     ]
    }
   ],
   "source": [
    "# 带条件的集合内涵\n",
    "sss = {i for i in s if i % 2 == 0}\n",
    "print(sss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'love', 'zhangwenhanzhangwenhan', 'iiii', 'ii', 'i', 'lovelovelovelove', 'zhangwenhanzhangwenhanzhangwenhan', 'zhangwenhan', 'iii', 'zhangwenhanzhangwenhanzhangwenhanzhangwenhan', 'lovelove', 'lovelovelove'}\n",
      "{'lovelove', 'zhangwenhanzhangwenhan', 'ii'}\n"
     ]
    }
   ],
   "source": [
    "# 多循环的集合内涵\n",
    "s1 = {1,2,3,4}\n",
    "s2 = {\"i\",\"love\",\"zhangwenhan\"}\n",
    "\n",
    "s = {m*n for m in s2 for n in s1}\n",
    "print(s)\n",
    "\n",
    "s = {m*n for m in s2 for n in s1 if n==2}\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 集合函数/关于集合的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "1222\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# len,max,min:跟其他基本函数一致\n",
    "s = {43,23,56,223,4,2,1222,4,323,1}\n",
    "print(len(s))\n",
    "print(max(s))\n",
    "print(min(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 23}\n"
     ]
    }
   ],
   "source": [
    "# set:生成一个集合\n",
    "l = [1,2,3,4,3,23,1,2,3,4]\n",
    "s = set(l)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 334}\n"
     ]
    }
   ],
   "source": [
    "# add:向集合内添加元素\n",
    "s = {1}\n",
    "s.add(334)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140369201707496\n",
      "140369201707496\n"
     ]
    }
   ],
   "source": [
    "# clear\n",
    "s = {1,2,3,4,5}\n",
    "print(id(s))\n",
    "s.clear()\n",
    "print(id(s))\n",
    "# 结果表明clear函数是原地清空数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 5, 23}\n",
      "{2, 3, 5, 23}\n",
      "********************\n",
      "{2, 3, 5, 23}\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "1100",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-848214939ea8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1100\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     16\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: 1100"
     ]
    }
   ],
   "source": [
    "# copy:拷贝\n",
    "# remove:移除指定的值,直接改变原有值，如果要删除的值不存在，报错\n",
    "# discard:移除集合中指定的值，跟remove一样，但是如果要删除的话，不报错\n",
    "\n",
    "s = {23,3,4,5,1,2,3}\n",
    "s.remove(4)\n",
    "print(s)\n",
    "s.discard(1)\n",
    "print(s)\n",
    "\n",
    "print(\"*\" * 20)\n",
    "s.discard(1100)\n",
    "print(s)\n",
    "\n",
    "s.remove(1100)\n",
    "print(s)\n",
    "\n",
    "# 为啥remove不存在的值会报keyerror"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "{2, 3, 4, 5, 6, 7}\n"
     ]
    }
   ],
   "source": [
    "# pop 随机移除一个元素\n",
    "s = {1,2,3,4,5,6,7}\n",
    "d = s.pop()\n",
    "print(d)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{5, 6}\n",
      "{1, 2, 3, 4}\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 集合函数\n",
    "# intersection：交集\n",
    "# difference：差集\n",
    "# union：并集\n",
    "# issubset：检查一个集合是否为另一个子集\n",
    "# issuperset：检查一个集合是否为另一个超集\n",
    "s1 = {1,2,3,4,5,6}\n",
    "s2 = {5,6,7,8,9}\n",
    "\n",
    "s_1 = s1.intersection(s2)\n",
    "print(s_1)\n",
    "\n",
    "s_2 = s1.difference(s2)\n",
    "print(s_2)\n",
    "\n",
    "s_3 = s1.issubset(s2)\n",
    "print(s_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4}\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'set' and 'set'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-7-753cf8acab87>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms_1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0ms_2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ms1\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0ms2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      8\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms_2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'set' and 'set'"
     ]
    }
   ],
   "source": [
    "# 集合的数学操作\n",
    "s1 = {1,2,3,4,5,6}\n",
    "s2 = {5,6,7,8,9}\n",
    "s_1 = s1 - s2\n",
    "print(s_1)\n",
    "\n",
    "s_2 = s1 + s2 # 不支持加号\n",
    "print(s_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# frozen set:冰冻集合\n",
    "- 冰冻集合就是不可以进行任何修改的集合\n",
    "- frozen set是一种特殊集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'frozenset'>\n",
      "frozenset()\n"
     ]
    }
   ],
   "source": [
    "# 创建\n",
    "s = frozenset()\n",
    "print(type(s))\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# dict字典\n",
    "- 字典是一种组合数据，没有顺序的组合数据，数据以键值对形式出现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n",
      "{'one': 1, 'two': 2, 'three': 3}\n",
      "{'one': 1, 'two': 2, 'three': 3}\n",
      "{'one': 1, 'two': 2, 'three': 3}\n",
      "{'one': 1, 'two': 2, 'three': 3}\n"
     ]
    }
   ],
   "source": [
    "# 字典的创建\n",
    "# 创建空字典1\n",
    "d = {}\n",
    "print(d)\n",
    "\n",
    "# 创建空字典2\n",
    "d = dict()\n",
    "print(d)\n",
    "\n",
    "# 创建有值的字典,每一组数据用冒号隔开，每一对键值对用逗号隔开\n",
    "d = {\"one\":1,\"two\":2,\"three\":3}\n",
    "print(d)\n",
    "\n",
    "# 用dict创建有内容字典1\n",
    "d = dict({\"one\":1,\"two\":2,\"three\":3})\n",
    "print(d)\n",
    "\n",
    "# 用dict创建有内容字典2\n",
    "# 利用关键字参数\n",
    "d = dict(one=1, two=2, three=3)\n",
    "print(d)\n",
    "\n",
    "#\n",
    "d = dict([(\"one\",1),(\"two\",2),(\"three\",3)])\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典的特征\n",
    "- 字典是序列类型，但是是无序序列，所有没有分片和索引\n",
    "- 字典中的数据每个都有键值对组成，即kv对\n",
    "    - key：必须是可哈希的值，比如int，strint，float，tuple，但是，list，set，dice不行\n",
    "    - value：任何值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典常见操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "{'one': 'eins', 'two': 2, 'three': 3}\n",
      "{'two': 2, 'three': 3}\n"
     ]
    }
   ],
   "source": [
    "# 访问数据\n",
    "d = {\"one\":1,\"two\":2,\"three\":3}\n",
    "# 注意访问格式\n",
    "# 中括号内是键值\n",
    "print(d[\"one\"])\n",
    "\n",
    "d[\"one\"] = \"eins\"\n",
    "print(d)\n",
    "\n",
    "# 删除某个操作\n",
    "# 使用del操作\n",
    "del d[\"one\"]\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "key\n"
     ]
    }
   ],
   "source": [
    "# 成员检测，in，not in\n",
    "# 成员检测检测的是key内容\n",
    "d = {\"one\":1,\"two\":2,\"three\":3}\n",
    "\n",
    "if 2 in d:\n",
    "    print(\"value\")\n",
    "    \n",
    "if \"two\" in d:\n",
    "    print(\"key\")\n",
    "\n",
    "if (\"two\",2) in d:\n",
    "    print(\"kv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "one 1\n",
      "two 2\n",
      "three 3\n",
      "one 1\n",
      "two 2\n",
      "three 3\n",
      "1\n",
      "2\n",
      "3\n",
      "one -- 1\n",
      "two -- 2\n",
      "three -- 3\n"
     ]
    }
   ],
   "source": [
    "# 遍历在python2 和 3 中区别比较大，代码不通用\n",
    "# 按key来使用for循环\n",
    "d = {\"one\":1,\"two\":2,\"three\":3}\n",
    "# 使用for循环，直接按key值访问\n",
    "for k in d:\n",
    "    print(k,d[k])\n",
    "    \n",
    "# 上述代码可以改写成如下\n",
    "for k in d.keys():\n",
    "    print(k,d[k])\n",
    "\n",
    "\n",
    "# 只访问字典的值\n",
    "for v in d.values():\n",
    "    print(v)\n",
    "    \n",
    "# 注意以下特殊用法\n",
    "for k,v in d.items():\n",
    "    print(k,\"--\",v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典生成式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'one': 1, 'two': 2, 'three': 3}\n",
      "{'two': 2}\n"
     ]
    }
   ],
   "source": [
    "d = {\"one\":1,\"two\":2,\"three\":3}\n",
    "\n",
    "# 常规字典生成式\n",
    "dd = {k:v for k,v in d.items()}\n",
    "print(dd)\n",
    "\n",
    "# 加限制条件的字典生成式\n",
    "dd = {k:v for k,v in d.items() if v % 2 == 0}\n",
    "print(dd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典相关函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'one': 1, 'two': 2, 'three': 3}\n"
     ]
    }
   ],
   "source": [
    "# 通用函数：len, max, min, dict\n",
    "# str(字典)：返回字典的字符串格式\n",
    "d = {\"one\":1,\"two\":2,\"three\":3}\n",
    "print(str(d))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict_items'>\n",
      "dict_items([('one', 1), ('two', 2), ('three', 3)])\n"
     ]
    }
   ],
   "source": [
    "# clear：清空字典\n",
    "# items：返回字典的键值对组成的元组格式\n",
    "\n",
    "d = {\"one\":1,\"two\":2,\"three\":3}\n",
    "i = d.items()\n",
    "print(type(i))\n",
    "print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict_keys'>\n",
      "dict_keys(['one', 'two', 'three'])\n"
     ]
    }
   ],
   "source": [
    "# keys: 返回字典的键组成的一个结构\n",
    "k = d.keys()\n",
    "print(type(k))\n",
    "print(k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict_values'>\n",
      "dict_values([1, 2, 3])\n"
     ]
    }
   ],
   "source": [
    "# values：同理，一个可迭代的结构\n",
    "v = d.values()\n",
    "print(type(v))\n",
    "print(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "1\n",
      "100\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'on333'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-35-ac7c24a8f3b6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0;31m# 体会以下代码跟上面代码的区别\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'on333'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m: 'on333'"
     ]
    }
   ],
   "source": [
    "# get: 根据指定键返回相应的值，好处是，可以设置默认值\n",
    "\n",
    "d = {\"one\":1,\"two\":2,\"three\":3}\n",
    "print(d.get(\"on333\"))\n",
    "\n",
    "# get默认值是None,可以设置\n",
    "print(d.get(\"one\",100))\n",
    "print(d.get(\"one333\",100))\n",
    "\n",
    "# 体会以下代码跟上面代码的区别\n",
    "print(d['on333'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'eins': 'hahahahahahah', 'zwei': 'hahahahahahah', 'dree': 'hahahahahahah'}\n"
     ]
    }
   ],
   "source": [
    "# fromkeys: 使用指定的序列作为键，使用一个值作为字典的所有的键的值\n",
    "l = [\"eins\",\"zwei\",\"dree\"]\n",
    "# 注意fromkeys两个参数的类型\n",
    "# 注意fromkeys的调用主体\n",
    "d = dict.fromkeys(l,\"hahahahahahah\")\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
